<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\index\controller;
use think\Db;

/**
 * Class Fenxiao
 * 二级分销业务
 * @package app\index\controller
 */
class Fenxiao extends  Base
{
    public function savedata(){
        $post=input('post.');
        $uid=$post['uid'];
        unset($post['uid']);
        $res=db('user')->where(['acid'=>1,'id'=>$uid])->update($post);
        if($res){
            $this->result(0,'保存成功','',0,'json');
        }else{
            $this->result(1,'保存失败','',0,'json');
        }
    }
    // 个人分销基本信息
    public function fxinfo(){
        $post=input('post.');
        $user=db('user')->where(['acid'=>1,'id'=>$post['uid']])->find();
        if($user['pid']!=0){
            $pinfo=db('user')->where(['acid'=>1,'id'=>$user['pid']])->find();
            $user['pnickname']=$pinfo['nickname'];
        }else{
            $user['pnickname']='总店';
        }
        $this->result(0,'信息查询成功',$user,0,'json');
    }
    //提现
    public function tixian(){
        $post=input('post.');
        $map=array(
            'acid'=>1
        );
        $vipinfo=db('user')->where($map)->where('id',$post['uid'])->find();
        if($vipinfo['yongjin']-$post['txmoney']<=0){
            $this->result(1,'佣金不足','',0,'json');
        }else{
            $res=db('user')->where($map)->where('id',$post['uid'])->setField('yongjin',$vipinfo['yongjin']-$post['txmoney']);
            if($res){
                $data=array(
                    'acid'=>1,
                    'uid'=>$post['uid'],
                    'txnum'=>$post['txmoney'],
                    'txtype'=>$post['txtype'],
                    'sqtime'=>time(),
                    'is_verify'=>0,
                );
                $res2=db('tixian')->insert($data);
                if($res2){
                    $this->result(0,'提现审核中','',0,'json');
                }
            }
        }
    }
    // 佣金
    public function yongjin(){
        $post=input('post.');
        $map=array(
            'acid'=>1
        );
        $data =db('yjmingxi')->where($map)->where('shouyi_uid',$post['uid'])->order('createtime DESC')->limit(120)->select();
        foreach ($data as $k => $v) {
            $vipinfo=db('user')->where($map)->where('id',$v['source_uid'])->find();
            $orderinfo=db('mediaorder')->where($map)->where('orderid',$v['orderid'])->find();
            if(!empty($orderinfo)){
                if($orderinfo['goodstype']=='course'){
                    $db=db('coursemenu');
                    $goodstype='知识课程';
                    $goodsinfo=$db->where($map)->where('id',$orderinfo['menuid'])->find();
                    $goodsinfo['name']=$goodsinfo['menuname'];
                }else if($orderinfo['goodstype']=='pxcourse'){
                    $db=db('pxcourse');
                    $goodstype='培训班课程';
                    $goodsinfo=$db->where($map)->where('id',$orderinfo['menuid'])->find();
                }else if($orderinfo['goodstype']=='mall'){
                    $db=db('goods');
                    $goodstype='实物商品';
                    $goodsinfo=$db->where($map)->where('id',$orderinfo['menuid'])->find();
                }else{
                    $goodsinfo['name']=$orderinfo['goodstype'];
                    $goodsinfo['id']=$orderinfo['menuid'];
                    $goodstype=$orderinfo['goodstype'];
                }
            }
            $data[$k]['gxruid']=$vipinfo['id'];
            $data[$k]['goodsname']=$goodsinfo['name'];
            $data[$k]['goodsid']=$goodsinfo['id'];
            $data[$k]['goodstype']=$goodstype;
            $data[$k]['nickname']=$vipinfo['nickname'];
            $data[$k]['addtime']=date('Y-m-d H:i:s',$orderinfo['paytime']);
        }
        if(!empty($data)){
            $this->result(0,'获取佣金明细数据成功',$data,0,'json');
        }else{
            $this->result(1,'暂无明细','',0,'json');
        }
    }
    // 我的团队
    public function myteam(){
        $post=input('post.');
        $map=array(
            'acid'=>1
        );
        $type = $post['type']?$post['type']:0;
        $userinfo=db('user')->where($map)->where('id',$post['uid'])->find();
        $vipid = $post['uid'];
        // 一级分销商
        $zttotal =db('user')->where($map)->where('pid',$vipid)->count();
        if ($type == 0) {
            $arr2 =db('user')->where($map)->where('pid',$vipid)->order('createtime DESC')->select();
            if(!empty($arr2)){
                foreach ($arr2 as $key => $v) {
                    if($v['is_fxs']=='1'){
                        $arr2[$key]['viptype']='分销商';
                    }elseif($v['is_dls']=='1'){
                        $arr2[$key]['viptype']='代理商';
                    }
                    else{
                        if($v['is_vip']=='1'){
                            $arr2[$key]['viptype']='会员';
                        }else{
                            $arr2[$key]['viptype']='普通用户';
                        }
                    }
                    $arr2[$key]['zctime']=date('Y-m-d H:i:s',$v['createtime']);
                }
                $this->result(0,'获取直推团队数据成功',$arr2,$zttotal,'json');
            }else{
                $this->result(1,'暂无直推团队','',0,'json');
            }
        }
        if ($type == 1) {
            $arr = array();
            $arr2 = array();
            $tmp =db('user')->where($map)->where('pid',$vipid)->order('createtime DESC')->select();
            foreach ($tmp as $v) {
                array_push($arr, $v['id']);
            }

            foreach ($arr as $key => $v) {
                $fxs2 = db('user')->where($map)->where('pid',$v)->order('createtime DESC')->select();
                if(!empty($fxs2)){
                    $cache[$key] =$fxs2;
                }
            }


            if(!empty($cache)){
                foreach ($cache as $key => $val) {
                    foreach ($val as $k => $v) {
                        if($v['is_fxs']=='1'){
                            $val[$k]['viptype']='分销商';
                        }elseif($v['is_dls']=='1'){
                            $val[$k]['viptype']='代理商';
                        }else{
                            if($v['is_vip']=='1'){
                                $val[$k]['viptype']='会员';
                            }else{
                                $val[$k]['viptype']='普通用户';
                            }
                        }
                        $val[$k]['zctime']=date('Y-m-d H:i:s',$v['createtime']);
                    }
                    foreach ($val as $k => $v) {
                        array_push($arr2, $v);
                    }
                }
//                var_dump($arr2);
//                die();
                $jtnum=count($arr2);
                $this->result(0,'获取间推团队数据成功',$arr2,$jtnum,'json');
            }else{
                $this->result(1,'暂无间推团队','',0,'json');
            }
        }
    }
    public function tgm(){
        $map=array(
            'acid'=>1,
        );
        $res=Db::name('set')
            ->where($map)
            ->find();
        $hbbc=[trim($res['fxhb1']?to_media_url($res['fxhb1']):''),trim($res['fxhb2']?to_media_url($res['fxhb2']):''),trim($res['fxhb3']?to_media_url($res['fxhb3']):'')];
//        $hbbc=array_filter($hbbc);
//        $hbbc=array_filter($hbbc, 'to_media_url');
        $this->result(0,'获取海报列表数据成功',$hbbc,0,'json');
    }
    // 合成分销海报
    public function hbhc(){
        $post=input('post.');
        if(!empty($post['hbimg'])){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $post['hbimg']);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output1 = curl_exec($curl);
            curl_close($curl);
            $hbimg=$output1;
        }
        $map=array(
            'acid'=>1,
        );
        $res=Db::name('set')
            ->where($map)
            ->find();
        $userinfo=Db::name('user')
            ->where($map)
            ->where('id',$post['userid'])
            ->find();
        if($userinfo['nickname']!='' && $userinfo['avatar']!=''){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $userinfo['avatar']);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            $avatar_str=$output;

            // $Qr_img=$this->getQr();//生成为公众号带参数临时二维码
            $h5_set=Db::name('set_h5')
                ->where($map)
                ->find();
            $url=$h5_set['url'].'?userid='.$userinfo['id'];
            $path = false;
            $level = 'L';
            $size = '8';
            $margin ='2';
            if($post['apptype']=='H5'){
                vendor("phpqrcode.phpqrcode");
                $qrcode = new \QRcode();
                ob_start();
                $qrcode->png($url, $path, $level, $size, $margin);
                $Qr_img =ob_get_contents();
                ob_end_clean();
            }elseif($post['apptype']=='MP-WEIXIN'){
                $access_token = $this->get_access_token('MP-WEIXIN');
                $access_token=json_decode($access_token,true);
                //$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token['access_token'];
                //                $data1 = [
                //                    'scene'=>$userinfo['id'],
                //                    //'page'=>"pages/index/index"
                //                    'is_hyaline'=>false
                //                ];
                $url="https://api.weixin.qq.com/wxa/getwxacode?access_token=".$access_token['access_token'];
                $data1 = [
                    'path'=>"pages/index/index?pid=".$userinfo['id'],
                    'is_hyaline'=>false
                ];
                $data=json_encode($data1);
                $Qr_img =$this->http_curl($url,'post',$data);
            }

            // 生成首页带参数链接二维码
            if ($avatar_str && $Qr_img) {
                $hbpath=$this->getFinal($userinfo['nickname'],$res['hbtext'],$avatar_str,$hbimg,$post['imgkey'],$Qr_img,$userinfo['id']);
                if(!empty($hbpath)){
                    $hbpath = to_media_url($hbpath);
                    $this->result(0,'合成海报成功',['imgpath'=>$hbpath],0,'json');
                }else{
                    $this->result(1,'合成失败','',0,'json');
                }
            }
        }else{
            $this->result(1,'合成失败','',0,'json');
        }
    }
    public function getFinal($nickname,$text,$avatar,$hbimg,$imgkey,$qrimg,$userid){
        $acid=1;
        //背景图
        $is_very = $hbimg;

        if(strlen($is_very) < 1)
        {
            return false;
        }
        $QR = imagecreatefromstring($is_very);
        //微信头像

        $wx_logo = imagecreatefromstring($avatar);
        $QR_width    = imagesx($QR);//背景图宽度
        $QR_height   = imagesy($QR);//背景图高度

        $wx_logo_width  = imagesx($wx_logo);
        $wx_logo_height = imagesy($wx_logo);

        //载入头像
        //imagecopyresampled($wx_logo,$QR, 0, 0, 0, 0, $wx_logo_width, $wx_logo_height,$QR_width,$QR_height);
        imagecopyresampled($QR, $wx_logo, $QR_width*0.03, $QR_height*0.7+10, 0, 0, 100, 100, $wx_logo_width, $wx_logo_height);
        //载入二维码
        $qr_img_string=$qrimg;
        $qr_img=imagecreatefromstring($qr_img_string);
        $qr_img_width  = imagesx($qr_img);
        $qr_img_height = imagesy($qr_img);
        imagecopyresampled($QR, $qr_img, $QR_width*0.5-100, $QR_height*0.5, 0, 0, 200, 200, $qr_img_width, $qr_img_height);
        //载入文字
        //字体
        $font_file = ROOT_PATH .'public/font/bdzy.ttf';
        //字体颜色
        $color = imagecolorallocate($QR, 255, 154, 40);
        $black = imagecolorallocate($QR, 255, 0, 0);
        // 昵称
        //imagefttext($QR, 30, 0, $QR_width*0.03+150, $QR_height*0.4+70, $black, $font_file,"我是".$nickname);
        // 邀请文字
        $map=array(
            'acid'=>1,
        );
        $res=Db::name('set')
            ->where($map)
            ->find();
        $yqtext=$nickname."诚邀您来".$res['title']."一起学习";
        imagefttext($QR, 23, 0, $QR_width*0.03, $QR_height*0.8+60, $black, $font_file,$yqtext);
//        // 画一条分隔线
        $blank = imagecolorallocate($QR,255,255,255);//创建一个颜色，以供使用
        imageline($QR,$QR_width*0.03,$QR_height*0.8+90,$QR_width*0.75,$QR_height*0.8+90,$blank);
        // 自定义文字
        $black2=imagecolorallocate($QR, 0, 0, 0);
        imagefttext($QR, 23, 0, $QR_width*0.03, $QR_height*0.8+145, $black2, $font_file,$text);

        //每次发送指令每次重新生成二维码
        $filedir=ROOT_PATH . 'public/hb/'.$acid.'/'.$userid.'/';
        is_dir($filedir) OR mkdir($filedir,0777,true);
        $file_path = $filedir.time()."-".$imgkey.'.jpg';
        $res2=imagejpeg($QR,$file_path);
        if($res2){
            $hbpath='/hb/'.$acid.'/'.$userid."/".time()."-".$imgkey.'.jpg';
            return $hbpath;
        }
        //销毁资源
        imagedestroy($QR);
    }
}
